<?php
/**
 *@file wapuser.php
 *@author Zhe.Zhou(zhouzhe@haibao.com)
 *@date 2015-10-26 下午5:23:04
 *@brief  
 */
namespace haibao\www\data;

class WapUser extends \haibao\www\data\BaseMysql{
	const TABLE_P = "hb_common_member_profile";
	private $defaultProfile = array('realname'=>0,'gender'=>0,'birthday'=>0,'residecity'=>0,'email'=>0,'qq'=>0);
	
	public function __construct(){
		$this->setConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3);
	}
	
	public function updateUserProfileByUserId($userId,$profile){
		$this->beginTranscation();
		try {
			$options = '';
			$privateInfo = array();
			foreach($profile as $key=>$value){
				if($key == "privacy" || $key == 'email' || $key == 'nickname'){
					$privateInfo[$key] = $value;
					unset($profile[$key]);
				}else{
					$options .= ",$key='$value'";
				}
			}
			if($options){
				$sql = "UPDATE ".self::TABLE_P.' set '.trim($options,',').' where uid=%s';
				$this->query($sql,array($userId));
			}
			
			if(!empty($privateInfo['email'])){
				$data = array($privateInfo['email'],$userId);
				$sqlMember = "update hb_common_member set email=%s where uid=%s";
				$sqlUcenter = "update hb_ucenter_members set email=%s where uid=%s";
				$this->query($sqlMember,$data);
				$this->query($sqlUcenter,$data);
			}
			
			$this->updatePrivacy($userId,$privateInfo['privacy']);
		}catch (\Exception $e) {
			$this->rollback();
			throw $e;
		}
		$this->endTranscation();
		
		//修改新用户表数据
		$this->setConfig( \haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER );
		$nickName = '';
		if(!empty($privateInfo['nickname'])){
			$nickName = trim($privateInfo['nickname']);
		}
		$gender = $privateInfo['privacy'][1];
		$setString = ' Gender=%s,NickName=%s';
		$setValueArr = array($gender,$nickName,$userId);
		$this->query('update UserReg set '.$setString.' where Id=%s', $setValueArr);
	}
	
	public function getPrivacyByUserId($userId){
		$resArr = array();
		$sql = "SELECT privacy FROM `hb_common_member_field_home` WHERE uid=%s";
		$result = $this->query($sql,array($userId))->fetch_assoc();
		if($result){
			$arr = unserialize($result['privacy']);
			$resArr = isset($arr['profile'])?array_merge($this->defaultProfile,$arr['profile']):$this->defaultProfile;
		}else{
			$resArr = $this->defaultProfile;
		}
		return $resArr;
	}
	
	public function getUserInfoByUserId($userId){
		$sql = "SELECT u.email,p.realname,p.gender,p.birthyear,p.birthmonth,p.birthday,p.resideprovince,p.constellation,p.qq
				FROM ".self::TABLE_P." p
				left join hb_ucenter_members u
				on p.uid = u.uid
				WHERE p.uid = %s";
		$result = $this->query($sql,array($userId));
		$resData = array();
		while($result->fetch_assoc()){
			$resData['id'] = $userId;
			$resData['realName'] = $result->getData('realname');
			$resData['gender'] = $result->getData('gender');
			$resData['birthYear'] = $result->getData('birthyear');
			$resData['birthMonth'] = $result->getData('birthmonth');
			$resData['birthDay'] = $result->getData('birthday');
			$resData['resideProvince'] = $result->getData('resideprovince');
			$resData['constellation'] = $result->getData('constellation');
			$resData['qq'] = $result->getData('qq');
			$resData['email'] = $result->getData('email');
		}
		return $resData;
	}
	
	public function updateAvatarByUid($uid,$userAvatar){
		
		if(!empty($userAvatar['avatar'])){
			$this->query("update hb_common_member set avatarstatus=%s where uid=%s", array(1, $uid));
			
			//修改新用户表数据
			$this->setConfig( \haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER );
			$setValueArr = array($userAvatar['avatar'],$userAvatar['avatar_md5'],$uid);
			$this->query('update UserReg set AvatarUrl=%s,AvatarMd5=%s where Id=%s', $setValueArr);
		}
		$this->endTranscation();
	}
	
	private function updatePrivacy($uid,$privacy){
		$sql = "SELECT privacy FROM `hb_common_member_field_home` WHERE uid=%s";
		$result = $this->query($sql,array($uid))->fetch_assoc();
		if($result){
			$arr = unserialize($result['privacy']);
			if($privacy){
				$newPrivacy = array('realname'=>$privacy[0],'gender'=>$privacy[1],'birthday'=>$privacy[2],'residecity'=>$privacy[3],'email'=>$privacy[4],'qq'=>$privacy[5]);
			}else{
				$newPrivacy = $this->defaultProfile;
			}
			
			$arr['profile'] = $newPrivacy;
			$storePrivacy = serialize($arr);
			$updateSql = "update `hb_common_member_field_home` set privacy = %s where uid=%s";
			$this->query($updateSql,array($storePrivacy,$uid));
		}
	}
	public function isEachFriend($fromUid,$toUid){
    	$this->setConfig( \haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER );
	    //$sql = "SELECT * FROM `hb_home_friend` WHERE uid=%s AND fuid=%s";
	    $sql = "SELECT * FROM UserAttentionRecord WHERE UserId = %s AND AttentionUserId = %s AND UserType = 0 AND Status = 0";
	    $result = $this->query($sql,array($fromUid,$toUid))->fetch_assoc();
	    return $result; 
	}
	
	
	public function addFridend($fromUid,$myUserName,$toUid,$friendUserName){
	    self::firstAddFriend($fromUid,$toUid,$friendUserName);
	    self::secondAddFriend($fromUid,$myUserName,$toUid);
	   
	}
	private function firstAddFriend($fromUid,$toUid,$friendUserName){
	    $sqlArr = $valArr = array();
	    $sql = "INSERT INTO hb_home_friend (uid,fuid,fusername,gid,num,dateline) VALUES";
	    array_push($sqlArr,'(%s,%s,%s,%s,%s,%s)');
	    array_push($valArr,$fromUid);
	    array_push($valArr,$toUid);
	    array_push($valArr,$friendUserName);
	    array_push($valArr,1);
	    array_push($valArr,0);
	    array_push($valArr,time());
	    if($sqlArr && $valArr){
	        $sql .= implode(',',$sqlArr);
	        $this->query($sql,$valArr);
	    }
	}
	private function secondAddFriend($fromUid,$myUserName,$toUid){
	    $sqlArr = $valArr = array();
	    $sql = "INSERT INTO hb_home_friend (uid,fuid,fusername,gid,num,dateline) VALUES";
	    array_push($sqlArr,'(%s,%s,%s,%s,%s,%s)');
	    array_push($valArr,$toUid);
	    array_push($valArr,$fromUid);
	    array_push($valArr,$myUserName);
	    array_push($valArr,1);
	    array_push($valArr,0);
	    array_push($valArr,time());
	    if($sqlArr && $valArr){
	        $sql .= implode(',',$sqlArr);
	        $this->query($sql,$valArr);
	    } 
	}
}